//
//  StatusPictureView.swift
//  wbxm
//
//  Created by apple on 15/12/20.
//  Copyright © 2015年 apple. All rights reserved.
//

import UIKit
private let pictureCellID = "pictureCellID"

private let pictureCellMargin: CGFloat = 5


class StatusPictureView: UICollectionView {
    
    
    
    //MARK:定义外部模型属性
    var imageURLs: [NSURL]?{
        didSet{
            //修正配图视图的大小
            let pSize = caclePictureViewSize()
            
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(pSize)
            }
            
            testLabel.text = "\(imageURLs?.count ?? 0)"
            
           reloadData()
        }
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        //设置流水布局
        let layout = UICollectionViewFlowLayout()
        //设置艰巨
        layout.minimumLineSpacing = pictureCellMargin
        layout.minimumInteritemSpacing = pictureCellMargin
        
        super.init(frame: frame, collectionViewLayout: layout)
        
        //注册cell
        self.registerClass(pictureCell.self, forCellWithReuseIdentifier: pictureCellID)
        //设置数据源
        self.dataSource = self
        //设置颜色
        backgroundColor = UIColor.whiteColor()
        //设置UI
         setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //计算配图视图的大小
    func caclePictureViewSize() -> CGSize {
        //获取配图数量
        let imageCount = imageURLs?.count ?? 0
        //获取配图的最大宽度
        let maxWidth = screenW - 2 * StatusCellMarigin
        let itemWidth = (maxWidth - 2 * pictureCellMargin) / 3
        
        //获取colletionView的流水布局
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        
        //没有图片
        if imageCount == 0 {
            return CGSizeZero
        }
        
        //1张图片
        if imageCount == 1 {
            //TODO: 单图会按照图片等比例显示
            //先给定固定尺寸
            let imageSize = CGSize(width: 180, height: 180)
            layout.itemSize = imageSize
            return imageSize
        }
        
        if imageCount == 4 {
            let w = itemWidth * 2 + pictureCellMargin
            return CGSize(width: w, height: w)
        }
        
        //如果走到这里, 就是其他的多种那个图片
//        1,2,3  -> 1
//        4,5,6  -> 2
//        7,8,9  -> 3
//        先 整体 + 1  -> 修正 分子 -1
        let row = CGFloat((imageCount - 1) / 3 + 1)
        return CGSize(width: maxWidth, height: row * itemWidth + (row - 1) * pictureCellMargin )

    }
    
    
    //MARK:设置页面和布局
    private func setupUI() {
        self.addSubview(testLabel)
        testLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
    }
    
    private lazy var testLabel: UILabel = UILabel(title: "", color: UIColor.redColor(), fontSize: 30)

}




//数据源方法
extension StatusPictureView: UICollectionViewDataSource {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageURLs?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureCellID, forIndexPath: indexPath) as! pictureCell
//        cell.backgroundColor = UIColor.randomeColor()
        
        cell.imageURL = imageURLs![indexPath.item]
        
        return cell
    }
    
 
}


class pictureCell: UICollectionViewCell {
    
    
    // MARK:定义外部模型属性
    var imageURL: NSURL? {
        didSet{
            iconView.sd_setImageWithURL(imageURL)
        }
    }
    
    
    // MARK:重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK:设置页面和布局
    private func setupUI() {
        
        contentView.addSubview(iconView)
        
        //设置自动布局
        iconView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(snp_edges)
            
        }
        
    }
    
    // MARK:懒加载所有的子视图
    private lazy var iconView: UIImageView = {
        let iv = UIImageView()
        //ScaleAspectFill 显示图片的原比例 但是 图片会被裁减   一般采用这种方式
        //ScaleToFill  图片默认的显示样式  但是 图片 会被压缩 或者拉伸 一般不推荐使用这种方式来显示
        iv.contentMode = UIViewContentMode.ScaleAspectFill
        //在手码开发中 图片的默认的裁剪 是没有开启的 需要手动开启
        iv.clipsToBounds = true
        
        return iv
        
    }()

    
    
    
    
    
}









